using Brick;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.GDK
{
	public class gdk_music
	{
		public static GameObject gameObject;

		public static gdk_music _instance;

		private AudioSource musicAudioSource;

		private AudioSource hitAudioSource;

		private AudioSource shexianAudioSource;

		private List<AudioSource> unusedSoundAudioSourceList;

		private List<AudioSource> usedSoundAudioSourceList;

		private Dictionary<string, AudioClip> audioClipDict;

		private float musicVolume = 1f;

		private float soundVolume = 1f;

		private string musicVolumePrefs = "MusicVolume";

		private string soundVolumePrefs = "SoundVolume";

		private int poolCount = 3;

		public static gdk_music Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new gdk_music();
				}
				return _instance;
			}
			set
			{
				_instance = value;
			}
		}

		public gdk_music()
		{
			unusedSoundAudioSourceList = new List<AudioSource>();
			usedSoundAudioSourceList = new List<AudioSource>();
			gameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
			gameObject.name = "[musicNode]";
			Object.DontDestroyOnLoad(gameObject);
			musicAudioSource = gameObject.AddComponent<AudioSource>();
			audioClipDict = new Dictionary<string, AudioClip>();
			musicVolume = GetMusicVolume();
			soundVolume = GetSoundVolume();
		}

		public void PlayMusic(string id, bool loop = true)
		{
			DOTween.To(() => musicAudioSource.volume, delegate(float value)
			{
				musicAudioSource.volume = value;
			}, 0f, 0.5f).OnComplete(delegate
			{
				musicAudioSource.clip = GetAudioClip(id);
				musicAudioSource.clip.LoadAudioData();
				musicAudioSource.loop = loop;
				musicAudioSource.volume = musicVolume;
				musicAudioSource.Play();
				DOTween.To(() => musicAudioSource.volume, delegate(float value)
				{
					musicAudioSource.volume = value;
				}, musicVolume, 0.5f);
			});
		}

		public void PlayHitSound()
		{
			if (!hitAudioSource)
			{
				hitAudioSource = gameObject.AddComponent<AudioSource>();
				hitAudioSource.clip = GetAudioClip(AudioID.hit);
				hitAudioSource.clip.LoadAudioData();
				hitAudioSource.volume = soundVolume;
				hitAudioSource.Play();
			}
			else if (!hitAudioSource.isPlaying)
			{
				hitAudioSource.volume = soundVolume;
				hitAudioSource.Play();
			}
		}

		public void PlaySheXianSound()
		{
			if (!shexianAudioSource)
			{
				shexianAudioSource = gameObject.AddComponent<AudioSource>();
				shexianAudioSource.clip = GetAudioClip(AudioID.shexian);
				shexianAudioSource.clip.LoadAudioData();
				shexianAudioSource.volume = soundVolume;
				shexianAudioSource.Play();
			}
			else if (!shexianAudioSource.isPlaying)
			{
				shexianAudioSource.volume = soundVolume;
				shexianAudioSource.Play();
			}
		}

		public void PlaySound(string id)
		{
			if (unusedSoundAudioSourceList.Count != 0)
			{
				AudioSource audioSource = UnusedToUsed();
				audioSource.clip = GetAudioClip(id);
				audioSource.clip.LoadAudioData();
				audioSource.Play();
				WaitPlayEnd(audioSource);
			}
			else
			{
				AddAudioSource();
				AudioSource audioSource2 = UnusedToUsed();
				audioSource2.clip = GetAudioClip(id);
				audioSource2.clip.LoadAudioData();
				audioSource2.volume = soundVolume;
				audioSource2.loop = false;
				audioSource2.Play();
				WaitPlayEnd(audioSource2);
			}
		}

		private IEnumerator WaitPlayEnd(AudioSource audioSource)
		{
			yield return new WaitUntil(() => !audioSource.isPlaying);
			UsedToUnused(audioSource);
		}

		private AudioClip GetAudioClip(string id)
		{
			if (!audioClipDict.ContainsKey(id))
			{
				AudioClip value = Resources.Load(id) as AudioClip;
				audioClipDict.Add(id, value);
			}
			return audioClipDict[id];
		}

		private AudioSource AddAudioSource()
		{
			if (unusedSoundAudioSourceList.Count != 0)
			{
				return UnusedToUsed();
			}
			AudioSource audioSource = gameObject.AddComponent<AudioSource>();
			unusedSoundAudioSourceList.Add(audioSource);
			return audioSource;
		}

		private AudioSource UnusedToUsed()
		{
			AudioSource audioSource = unusedSoundAudioSourceList[0];
			unusedSoundAudioSourceList.RemoveAt(0);
			usedSoundAudioSourceList.Add(audioSource);
			return audioSource;
		}

		private void UsedToUnused(AudioSource audioSource)
		{
			usedSoundAudioSourceList.Remove(audioSource);
			if (unusedSoundAudioSourceList.Count >= poolCount)
			{
				UnityEngine.Object.Destroy(audioSource);
			}
			else
			{
				unusedSoundAudioSourceList.Add(audioSource);
			}
		}

		public void ChangeMusicVolume(float volume)
		{
			musicVolume = volume;
			musicAudioSource.volume = volume;
			PlayerPrefs.SetFloat(musicVolumePrefs, volume);
		}

		public void ChangeSoundVolume(float volume)
		{
			soundVolume = volume;
			for (int i = 0; i < unusedSoundAudioSourceList.Count; i++)
			{
				unusedSoundAudioSourceList[i].volume = volume;
			}
			for (int j = 0; j < usedSoundAudioSourceList.Count; j++)
			{
				usedSoundAudioSourceList[j].volume = volume;
			}
			PlayerPrefs.SetFloat(soundVolumePrefs, volume);
		}

		public float GetMusicVolume()
		{
			float result = 1f;
			if (gdk_localStorage.hasKey(musicVolumePrefs))
			{
				result = gdk_localStorage.getFloat(musicVolumePrefs);
			}
			return result;
		}

		public float GetSoundVolume()
		{
			float result = 1f;
			if (gdk_localStorage.hasKey(soundVolumePrefs))
			{
				result = gdk_localStorage.getFloat(soundVolumePrefs);
			}
			return result;
		}
	}
}
